package
{
	import com.tenoz.display3d.CurvingRoadTest3;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import net.hires.debug.Stats;
	
	[SWF(width=955, height=410, backgroundColor="#0000FF", frameRate=30)]
	public class Main extends Sprite
	{
		private var loader:Loader;
		private var imageURL:String = "../assets/ground.png";
		
		private var road:CurvingRoadTest3;
		
		public function Main()
		{
			addChild(new Stats());
			
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageComplete);
			
			loader.load(new URLRequest(imageURL));
		}
		
		public var speed:Number = 0;
		public var accel:Number = 1;
		public var maxSpeed:Number = 30;
		public var minSpeed:Number = -2;
		private function onTick(e:Event):void
		{
			road.pos += speed;
		}
		
		private function onImageComplete(e:Event):void
		{
			var bmd:BitmapData = Bitmap(loader.content).bitmapData;
			
			road = new CurvingRoadTest3(bmd);
			var rect:Rectangle = road.getBounds(road);
			this.addChild(road);
			this.addEventListener(Event.ENTER_FRAME, onTick);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
			clearInterval(ID);
			ID = setInterval(accelorate, 200);
		}
		
		private function accelorate():void
		{
			if(keyUp)
			{
				speed = (speed < maxSpeed)? speed+accel : maxSpeed;
			}
			if(keyDown)
			{
				speed = (speed > minSpeed)? speed-accel : minSpeed;
			}
		}
		
		public var ID:int;
		public var keyUp:Boolean = false;
		public var keyDown:Boolean = false;
		private function onKeyDown(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.UP)
			{
				keyUp = true;
			}
			else if(e.keyCode == Keyboard.DOWN)
			{
				keyDown = true
			}
		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.UP)
			{
				keyUp = false;
			}
			else if(e.keyCode == Keyboard.DOWN)
			{
				keyDown = false
			}
		}
	}
}